<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<div class="flex items-center justify-between py-2">
				<div>
					<el-space>
						<el-popover trigger="click" :width="320" placement="bottom-start">
							<el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList" ref="queryRef">
								<el-form-item label="类型" prop="deviceTypeId">
									<el-select class="w100" clearable v-model="state.queryForm.deviceTypeId" :teleported="false" @change="handleTypeChange">
										<el-option :key="item.id" :label="item.typeName" :value="item.id" v-for="item in deviceTypeList" :disabled="item.state === 20" />
									</el-select>
								</el-form-item>
								<el-form-item label="型号" prop="deviceModelId">
									<el-select class="w100" clearable v-model="state.queryForm.deviceModelId" :teleported="false">
										<el-option
											:key="item.id"
											:label="item.deviceModelName"
											:value="item.id"
											v-for="item in deviceModelList"
											:disabled="item.state === 20"
										/>
									</el-select>
								</el-form-item>
							</el-form>
							<template #reference>
								<el-button link type="primary"
									>高级搜索<el-icon><ArrowDown /></el-icon>
								</el-button>
							</template>
						</el-popover>
						<el-input placeholder="请输入IMEI" v-model="state.queryForm.imei" />
						<el-button type="primary" @click="getDataList(true)" icon="search">查询</el-button>
						<el-button @click="resetQuery()" icon="refresh"></el-button>
					</el-space>
				</div>
				<div>
					<el-button icon="circlePlus" type="primary" :disabled="multiple" @click="handleOutInv()">批量出库</el-button>
					<el-tooltip placement="bottom">
						<span class="text-lg font-semibold text-warning">
							<el-icon class="ml-2 mr-4"><Warning /></el-icon>
						</span>
						<template #content>
							<div class="w-[600px] p-4 tips-box">
								<div class="mb-2 text-lg font-bold">出库资费说明：</div>
								<h4>初始化</h4>
								<p>
									1、勾选设备初始化时，出库时将同步清空测试阶段的事件记录、报警记录、故障记录、
									并将设备状态重置为未激活。如不需要清空以上数据，请取消勾选项。
								</p>
								<h4>代付</h4>
								<p>1、不实时扣除本单位短信及语音余额，实付实扣模式；</p>
								<p>2、该模式下短信或语音发送受本单位余额影响；请保证本单位的余额足够；</p>
								<p>3、可配合设置最高代付多少数量或代付到期时间，其中一个条件到达后停止代付，由所 属单位自行付费；</p>
								<h4>配送</h4>
								<p>1、该模式下将实时预扣本单位的短信及语音余额，扣除成功后设备后续通 讯费不受本单位余额影响；</p>
								<p>2、分配数量指给每台设备分配的短信或语音数，分配的数量使用完后将 由所属单位自行支付；</p>
								<h4>退费说明</h4>
								<p>1、出库后首次安装30天内，且未使用系统配送的通讯包的，将全额退回平台费；</p>
								<p>2、已使用过系统配送的通讯包的，扣除1元通讯费，其他费用返还到账户；</p>
								<p>3、出库且安装超过30天的，不退回平台费；</p>
								<p>4、出库90天内但未安装的，初始化后平台费将退回当前账户；</p>
								<p>5、出库超过90天但未安装的，不退回平台费。</p>
							</div>
						</template>
					</el-tooltip>
				</div>
			</div>
			<!-- <el-row>
				<div class="mb8" style="width: 100%">
					<el-button icon="folder-add" type="primary" class="ml10" @click="formDialogRef.openDialog()" v-auth="'platform_basicInventoryList_add'">
						新 增
					</el-button>
					<el-button
						plain
						:disabled="multiple"
						icon="Delete"
						type="danger"
						v-auth="'platform_basicInventoryList_del'"
						@click="handleDelete(selectObjs)"
					>
						删除
					</el-button>
					<right-toolbar
						v-model:showSearch="showSearch"
						:export="'platform_basicInventoryList_export'"
						@exportExcel="exportExcel"
						class="ml10 mr20"
						style="float: right"
						@queryTable="getDataList"
					></right-toolbar>
				</div>
			</el-row> -->
			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
				@selection-change="selectionChangHandle"
				@sort-change="sortChangeHandle"
			>
				<el-table-column type="selection" width="40" align="center" />
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="imei" label="IMEI" show-overflow-tooltip />
				<el-table-column prop="deviceType" label="设备类型" show-overflow-tooltip />
				<el-table-column prop="deviceModelName" label="设备型号" show-overflow-tooltip />
				<el-table-column prop="createTime" label="生产日期" show-overflow-tooltip />
				<el-table-column label="操作" width="150">
					<template #default="scope">
						<el-button type="primary" @click="handleOutInv(scope.row)" size="small">出库</el-button>
						<!-- <el-button icon="edit-pen" text type="primary" v-auth="'platform_basicInventoryList_edit'" @click="formDialogRef.openDialog(scope.row.id)"
							>编辑</el-button
						>
						<el-button icon="delete" text type="danger" v-auth="'platform_basicInventoryList_del'" @click="handleDelete([scope.row.id])"
							>删除</el-button
						> -->
					</template>
				</el-table-column>
			</el-table>
			<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
		</div>

		<!-- 编辑、新增  -->
		<form-dialog ref="formDialogRef" @refresh="getDataList(false)" />
		<!-- 出库 -->
		<out-storage-dialog ref="outStorageDialogRef" @refresh="getDataList(false)"></out-storage-dialog>
	</div>
</template>

<script setup lang="ts" name="systemBasicInventoryList">
import { BasicTableProps, useTable } from '/@/hooks/table';
import { fetchList, delObjs } from '/@/api/platform/basicInventoryList';
import { useMessage, useMessageBox } from '/@/hooks/message';
import { fetchListAll as fetchDeviceModelList } from '/@/api/platform/basicDeviceModel';
import { fetchListAll as fetchDeviceTypeList } from '/@/api/platform/basicDeviceType';

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const OutStorageDialog = defineAsyncComponent(() => import('./components/outStorage.vue'));
// 定义查询字典

// 定义变量内容
const formDialogRef = ref();
const outStorageDialogRef = ref();
// 搜索变量
const queryRef = ref();
const showSearch = ref(true);
const deviceTypeList: any = ref([]);
const deviceModelList: any = ref([]);
// 多选变量
const selectObjs = ref([]) as any;
const multiple = ref(true);

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {},
	pageList: fetchList,
	descs: ['create_time'],
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, sortChangeHandle, downBlobFile, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
	// 清空搜索条件
	queryRef.value?.resetFields();
	// 清空多选
	selectObjs.value = [];
	state.queryForm.imei = '';
	getDataList();
};

// 导出excel
const exportExcel = () => {
	downBlobFile('/platform/basicInventoryList/export', Object.assign(state.queryForm, { ids: selectObjs }), 'basicInventoryList.xlsx');
};

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
	selectObjs.value = objs;
	multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm('此操作将永久删除');
	} catch {
		return;
	}

	try {
		await delObjs(ids);
		getDataList();
		useMessage().success('删除成功');
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};

const handleOutInv = (row?: any) => {
	if (row) {
		outStorageDialogRef.value.openDialog([row]);
		return;
	}
	outStorageDialogRef.value.openDialog(selectObjs.value);
};
// 查询设备型号
const getModelList = (params) => {
	fetchDeviceModelList(params).then((res) => {
		deviceModelList.value = res.data;
	});
};
// 查询设备类型
const getTypeList = () => {
	fetchDeviceTypeList({ state: 20 }).then((res) => {
		deviceTypeList.value = res.data;
	});
};
getTypeList();
// 选择设备类型-查询设备型号
const handleTypeChange = (e) => {
	state.queryForm.deviceModelId = null;
	getModelList({ deviceTypeId: e, state: 20 });
};
</script>
<style lang="scss">
.tips-box {
	h4 {
		font-weight: bold;
	}
	p {
		padding-left: 2em;
	}
}
</style>
